﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static List<PorteVeiculoVO> getPorteVeiculos()
        {
            List<PorteVeiculoVO> porteVeiculos = getPorteVeiculos(false);
            return porteVeiculos;
        }

        public static List<PorteVeiculoVO> getPorteVeiculos(bool somentePermitidos)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdPorteVeiculo, PorteVeiculo, Permitido ");
            query.AppendLine("FROM porteveiculo ");
            if (somentePermitidos)
                query.AppendLine("WHERE Permitido=?Permitido ");
            query.AppendLine("ORDER BY PorteVeiculo ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (somentePermitidos)
                cmd.Parameters.AddWithValue("?Permitido", somentePermitidos);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<PorteVeiculoVO> listPorteVeiculo = new List<PorteVeiculoVO>();

                while (reader.Read())
                {
                    PorteVeiculoVO porteVeiculoVO = PorteVeiculoVO.CreatePorteVeiculoVO();
                    porteVeiculoVO.idPorteVeiculo = reader.GetInt32("IdPorteVeiculo");
                    porteVeiculoVO.porteVeiculo = reader.GetString("PorteVeiculo");
                    porteVeiculoVO.permitido = reader.GetBoolean("Permitido");

                    listPorteVeiculo.Add(porteVeiculoVO);
                }
                reader.Close();
                return listPorteVeiculo;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setPorteVeiculo(PorteVeiculoVO porteVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE porteveiculo ");
            query.AppendLine("SET PorteVeiculo=?PorteVeiculo, Permitido=?Permitido ");
            query.AppendLine("WHERE IdPorteVeiculo=?IdPorteVeiculo ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdPorteVeiculo", porteVeiculoVO.idPorteVeiculo);
            cmd.Parameters.AddWithValue("?PorteVeiculo", porteVeiculoVO.porteVeiculo);
            cmd.Parameters.AddWithValue("?Permitido", porteVeiculoVO.permitido);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newPorteVeiculo(PorteVeiculoVO porteVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO porteveiculo ");
            query.AppendLine("SET PorteVeiculo=?PorteVeiculo, Permitido=?Permitido ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?PorteVeiculo", porteVeiculoVO.porteVeiculo);
            cmd.Parameters.AddWithValue("?Permitido", porteVeiculoVO.permitido);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delPorteVeiculo(PorteVeiculoVO porteVeiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM porteveiculo ");
            query.AppendLine("WHERE IdPorteVeiculo=?IdPorteVeiculo ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdPorteVeiculo", porteVeiculoVO.idPorteVeiculo);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static bool readyToDeletePorteVeiculo(int idPorteVeiculo)
        {
            int contagem = 0;

            MySqlDataReader reader = null;
            StringBuilder query = new StringBuilder();
            MySqlCommand cmd = getConnection();
            cmd.Connection.Open();

            try
            {

                query.AppendLine("SELECT count(*) contagem FROM modelo WHERE IdPorteVeiculo=?IdPorteVeiculo ");
                cmd.CommandText = query.ToString();
                cmd.Parameters.AddWithValue("?IdPorteVeiculo", idPorteVeiculo);
                reader = cmd.ExecuteReader();
                reader.Read();
                contagem = reader.GetInt32("contagem");
                reader.Close();

                query = new StringBuilder();
                query.AppendLine("SELECT count(*) contagem FROM veiculo WHERE IdPorte=?IdPorteVeiculo ");
                cmd.CommandText = query.ToString();
                reader = cmd.ExecuteReader();
                reader.Read();
                contagem += reader.GetInt32("contagem");
                reader.Close();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

            return contagem == 0;

        }

    }
}
